﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using agile_tool.userControl;
using System.Windows.Media.Animation;

namespace agile_tool
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        projectPage pi;
        int userId;
        public static List<object> stateList = new List<object>();
        bool shake = true;
        public MainWindow()
        {
            InitializeComponent();
            refreshNavButton();
            stateList.Add(this);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            try
            {
                var a = from s in db.tblUsers where s.name == textBox1.Text && s.name == textBox2.Password select s;

                userId = a.First().uId;
                tbName.Text = a.First().userName;
                pi = new projectPage(userId, "");
                spBody.Children.Add(pi);
                stateList.Add(pi);
                refreshNavButton();

                Storyboard cl = (Storyboard)Resources["closeTheLogin"];
                cl.Begin();
                cl.Completed += new EventHandler(cl_Completed);
              
                tbSeatch.Text = "";
                btnLogout.Visibility = Visibility.Visible;
            }
            catch { textBox1.Text = "";

            textBox2.Clear(); 
            Storyboard rtt = (Storyboard)Resources["rtt"];
            rtt.Begin();
            }

            /* 
            DataClasses1DataContext db = new DataClasses1DataContext();

            tblUser tb = new tblUser();
            tb.name = textBox1.Text;
            tb.password = textBox2.Text;

            db.tblUsers.InsertOnSubmit(tb);

            db.SubmitChanges();

            */
        }

        void cl_Completed(object sender, EventArgs e)
        {
            //btnLogout.Visibility = Visibility.Visible;
            //pi = new projectPage(userId);
            //spBody.Children.Add(pi);
        }

        public void refreshNavButton()
        {

            shake = true;
            spNavigation.Children.Clear();
            buttonForNavigation.count = 0;
            buttonForNavigation nav = new buttonForNavigation("Home");
            nav.MouseDown += new MouseButtonEventHandler(nav2_MouseDown2);
            nav.Margin = new Thickness(0, 0, 0, 0);
            spNavigation.Children.Add(nav);

            foreach (object obj in stateList)
            {
                try
                {
                    nav = new buttonForNavigation(((IGenericPagesData)obj).getNavigationName());
                    spNavigation.Children.Add(nav);
                    nav.MouseDown += new MouseButtonEventHandler(nav2_MouseDown);
                }
                catch { }
            }
        }

        void nav2_MouseDown2(object sender, MouseButtonEventArgs e)
        {
            spBody.Children.Clear();
            stateList.Clear();
            refreshNavButton();
            btnProjects.IsEnabled = true;
            btnResource.IsEnabled = true;
            stateList.Add(this);
            Storyboard cl = (Storyboard)Resources["openTheLogin"];
            cl.Begin();
            btnLogout.Visibility = Visibility.Collapsed;
        }
       
        void nav2_MouseDown(object sender, MouseButtonEventArgs e)
        {
            buttonForNavigation nav = (buttonForNavigation)sender;
            try
            {
                int cnt = stateList.Count;
                for (int i = cnt; i > nav.num; i--)
                {
                    spBody.Children.Remove((UIElement)stateList[i - 1]);
                    stateList.RemoveAt(i - 1);
                }
                ((UIElement)stateList[nav.num -1]).Visibility = Visibility.Visible;
                refreshNavButton();
            }
            catch { MessageBox.Show("doesn't work"); }
        }
        

        private void btnLogout_Click_1(object sender, RoutedEventArgs e)
        {
            spBody.Children.Clear();
            stateList.Clear();
            refreshNavButton();
            stateList.Add(this);
            Storyboard cl = (Storyboard)Resources["openTheLogin"];
            cl.Begin();
            btnLogout.Visibility = Visibility.Collapsed;
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            ((IGenericPagesData)stateList.Last()).refresh(tbSeatch.Text);
        }

        private void btnResource_Click(object sender, RoutedEventArgs e)
        {
            btnProjects.IsEnabled = false;
            btnResource.IsEnabled = false;
            resourcePage rp = new resourcePage(1, "");
            spBody.Children.Add(rp);
            stateList.Add(rp);
            refreshNavButton();

            Storyboard cl = (Storyboard)Resources["closeTheLogin"];
            cl.Begin();
        }

        private void btnProjects_Click(object sender, RoutedEventArgs e)
        {
            btnProjects.IsEnabled = false;
            btnResource.IsEnabled = false;
            ((Button)sender).IsEnabled = false;
            pi = new projectPage(1, "");
            spBody.Children.Add(pi);
            stateList.Add(pi);
            refreshNavButton();

            Storyboard cl = (Storyboard)Resources["closeTheLogin"];
            cl.Begin();
        }

        private void brdrLogin_MouseEnter(object sender, MouseEventArgs e)
        {
            //Storyboard cl = (Storyboard)Resources["rtt"];
            //cl.Begin();
        }
       
        private void btnResource_MouseEnter(object sender, MouseEventArgs e)
        {
            if (shake)
            {
                shake = false;
                Storyboard cl = (Storyboard)Resources["rtt"];
                cl.Begin();
            }
        }

        private void btnProjects_MouseEnter(object sender, MouseEventArgs e)
        {
            if (shake)
            {
                shake = false;
                Storyboard cl = (Storyboard)Resources["rtt"];
                cl.Begin();
            }
        }
    }
}
